/**
 * 公告详情
 */
angular.module('inspinia').controller("deliverNoticeCtrl", function($scope, $http, $state, $stateParams) {
	//数据源
	$scope.sysType=[{text:"商户",value:'1'},{text:"代理商",value:'2'}];
//	$scope.receiveType=[{text:"直营所有级别代理商",value:'1'},{text:"所有一级代理商",value:'2'},{text:"直营一级代理商",value:'3'},{text:"非直营一级代理商",value:'4'}];
	$scope.agentBusiness = [{text:"全部",value:'0'},{text:"指定一级代理商下（包括下级发展）的商户",value:'1'}];
	$scope.baseInfo = {agentBusiness:'0'};
	$scope.isAll = [{text:'所有代理商',value:'1'},{text:'所有一级代理商',value:'2'}];
    $scope.oemTypes=[];//缓存所有指定组织
    $scope.oemListResult=false;
    $scope.oemListes=[];
	$scope.$state = $state;

	$scope.noticeTypes=[{text:"图片类",value:'1'},{text:"图文类",value:'0'}];
	//是否带参数
	$scope.isWithParamList=[{text:"是",value:'1'},{text:"否",value:'0'}];

	//业务产品表格
	$scope.productsGrid = { // 配置表格
		data : 'customerData',
		enableHorizontalScrollbar : 0, // 去掉滚动条
		enableVerticalScrollbar : 1, // 去掉滚动条
		columnDefs : [ // 表格数据
		               {field : 'teamName',displayName : '所属组织'},
		               {field : 'bpName',displayName : '业务产品名称'},
		               {field : 'remark',displayName : '业务产品说明'}],
		onRegisterApi : function(gridApi) {
			$scope.gridApiProduct = gridApi;
		}
	};
	
	var id = $stateParams.id;
	
	$http.get('notice/selectById/'+id).success(function(msg){
		$scope.noticeInfo = msg.notice;
		/*$scope.customerData = msg.allProduct;*/
		$scope.customerData = msg.products;
        $scope.oemTypes=msg.oemTypes;
        $scope.oemListes=msg.oemListes;
		$scope.selectAgentsGrid.data = msg.selectAgents;
		if(msg.notice.receiveType == '1'){
			$scope.baseInfo.team = '0';
			$scope.baseInfo.isAll = '1';
		}
		if(msg.notice.receiveType == '2'){
			$scope.baseInfo.team = '0';
			$scope.baseInfo.isAll = '2';
		}
		if(msg.notice.agentNo != '0'){
			$scope.baseInfo.agentBusiness = '1';
		}
        if(msg.notice.oemType.indexOf("11")!=-1){
            $scope.oemListResult=true;
        }
	}).error(function(){
	});
	
    $scope.deliverNotice = function(){
    	$scope.submitting = true;
    	$http.get('notice/deliverNotice/' + id).success(function(msg){
    		$scope.submitting = false;
    		if(msg.status){
    			$scope.notice(msg.msg);
    			$state.transitionTo('sys.queryNotice',null,{reload:true});
    		}else{
    			$scope.notice(msg.msg);
    		}
    		
    	}).error(function(){
    		$scope.notice("下发失败！");
    		$scope.submitting = false;
    	});
    }

	$scope.selectAgentsGrid = { // 配置表格
		data: null,
		enableHorizontalScrollbar: 0, // 去掉滚动条
		enableVerticalScrollbar: 1, // 去掉滚动条
		columnDefs: [
			{ field: 'agentName', displayName: '代理商名称' }
			, { field: 'agentNo', displayName: '代理商编号' }
			, { field: 'action',displayName: '操作',pinnable: false,sortable: false,editable:true,cellTemplate:
					"<div class='lh30'><a ng-click='grid.appScope.delAgent(row.entity)'>删除</a></div>"}
		],
		onRegisterApi: function (gridApi) {
			$scope.selectAgentsGridApi = gridApi;
		},
		isRowSelectable: function(row){ // 选中行
			//console.log("所属组织表格，选中了组织ID:" + row.entity.teamId)
		}
	};
}).filter('trustHtml', function ($sce) {
    return function (input) {
        return $sce.trustAsHtml(input);
    }
});
